<?php

namespace App\Services;

use Hprose\Http\Client;

class MicroService
{
    protected $requestService;

    public function __construct (RequestService $requestService)
    {
        $this->requestService = $requestService;
    }

    /**
     * @desc 根据传入的多条件参数
     * @param array $params ： include [id content subject_id and so on]
     * @return \Illuminate\Http\JsonResponse
     */
    public function esSearch ($params)
    {
        try {
            $service = new Client(config ('common.config.youdao_micro_service').'elasticService', false);
            $data = $service->search ($params);
            return response ()->json (['data' => $data, 'msg' => 'success', 'code' => 201], 201);
        } catch (\Exception $exception) {
            return response ()->json (['msg' => $exception->getMessage (), 'code' => 500], 500);
            // return response ()->json (['msg' => '查询出错，请稍后再试', 'code' => 500], 500);
        }
    }

    public function esUpdate ()
    {
        try {
            $service = new Client(config ('common.config.youdao_micro_service').'elasticService', false);
            $data = $service->update ();
            return response ()->json (['msg' => 'data update success', 'code' => 201, 'data' => $data], 201);
        } catch (\Exception $exception) {
            return response ()->json (['msg' =>  $exception->getMessage (), 'code' => 500], 500);
        }
    }

    // es 写入 task 创建
    public function esCreate ()
    {
        try {
            $service = new Client(config ('common.config.youdao_micro_service').'taskService', false);
            $taskId = $service->esCreate ();
            return response ()->json ([ 'msg' => 'data task add success', 'code' => 201, 'task_id' => $taskId], 201);
        } catch (\Exception $exception) {
            // return response ()->json (['msg' => '新增数据出错，请稍后再试', 'code' => 500], 500);
            return response ()->json (['msg' => $exception->getMessage (), 'code' => 500], 500);
        }

    }

    // es 写入状态查询
    public function esStatus ()
    {
        try {
            $service = new Client(config ('common.config.youdao_micro_service').'taskService', false);
            $data = $service->esCreateStatus ();
            return response ()->json ([ 'data' => $data, 'code' => 201], 201);
        } catch (\Exception $exception) {
            // return response ()->json (['msg' => '新增数据出错，请稍后再试', 'code' => 500], 500);
            return response ()->json (['msg' => $exception->getMessage (), 'code' => 500], 500);
        }
    }

    // 如果taskId 曾经验证过，在传入数据时使用之前传入的字符串
    public function handleContent ($taskId, $content)
    {
        return trim ($content);
        // $taskData = Task::where('task_id', $taskId)->find();
        // return isset($taskData) ? $taskData->content : trim($content);
    }

    // youdao 成功后的数据
    public function youdaoNotice ($taskId, $type, $content)
    {
        return [];
    }

}